// service -> user.js
const Service = require('egg').Service
const {genSaltPassword,comparePassword} = require('../extend/api')
class UserService extends Service {
    async signup(params) {
    // 从数据库里查询
        const res = await this.ctx.model.Admin.find({userName:params.username})
        if(res.length){
            return {
                msg:'用户已注册',
                code:401
            }
        }
        const hash = await genSaltPassword(params.password)
        await this.ctx.model.Admin.create({userName:params.username,passWord:hash,userPower:0})
        return {
            data:{},
            msg:'注册成功',
            code:200
        }
}

//登录函数
    async signin(params){
        const res = await this.ctx.model.Admin.find({
            userName:params.username
        })
        if(!res.length){
            return {
                msg:'用户不存在',
                code:404
            }
        }
        const isMatch = await comparePassword(params.password,res[0].passWord)
            if(!isMatch){
                return {
                    msg:'用户名或密码错误',
                    code:401
                }
            }
            // 密码匹配，创建token
            const token = this.ctx.app.jwt.sign({...res},this.ctx.app.config.jwt.secret,{
                expiresIn:'1h'
            })
            this.ctx.cookies.set('token',token,{
                maxAge:86400000
            })
            return {
                data:{
                    token,
                    userName:res[0].userName,
                    userPower:res[0].userPower,
                    userId:res[0].id,
                    avatar:res[0].avatar
                },
                msg:'登录成功',
                code:200
            }
    }

    async signout(){
        this.ctx.cookies.set('token',"",{
            maxAge:0
        })
        return {
            msg:'退出登录成功',
            code:200
        }
    }

    async getUserData(){
        const res = await this.ctx.model.Admin.find({},{passWord:0})
        return {
            msg:'查询用户成功',
            data:res
        }
    }
    
    async silenceUser(param){
        const id = param.id
        const Time = param.data
        await this.ctx.model.Admin.updateMany({"_id":{$in:id}},Time)
        const res = await this.ctx.model.Admin.find({},{passWord:0})
        return {
            msg:'禁言成功',
            data:res
        }
    }
      
    async bannedUser(param){
        const id = param.id
        const Time = param.data
        await this.ctx.model.Admin.updateMany({"_id":{$in:id}},Time)
        const res = await this.ctx.model.Admin.find({},{passWord:0})
        return {
            msg:'封号成功',
            data:res
        }
    }

    async changePassWord(param){
        const id = param.id
        const hash = await genSaltPassword(param.passWord)
        const res = await this.ctx.model.Admin.updateOne({"_id":id},{passWord:hash})
        console.log(res);
        return {
            msg:'修改密码成功',
            data:{}
        }
    }
}

module.exports = UserService